package com.aurora_manito.xiaomi.Controller;


import com.aurora_manito.xiaomi.entity.Address;
import com.aurora_manito.xiaomi.entity.Cart;
import com.aurora_manito.xiaomi.entity.User;
import com.aurora_manito.xiaomi.service.AddressService;
import com.aurora_manito.xiaomi.service.impl.AddressServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Aurora_Manito
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/xiaomi/address")
public class AddressController {
    @Autowired
    public AddressService addressService;

    //向self_info地址添加页面添加地址 12:48
    @RequestMapping(value = "/getAddress", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView addCartAjax(HttpSession session, ModelAndView modelAndView) {
        modelAndView.setViewName("forward:/self_info.jsp");
        User user = (User) session.getAttribute("user");
        //判断是否登录
        if (user == null) {
            return new ModelAndView("redirect:/login.jsp");
        }
        //获取收货地址
        List<Address> addressList = addressService.findByUid(user.getId());
        modelAndView.addObject("addList", addressList);

        return modelAndView;
    }

    @PostMapping(value = "/addaddress", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView addaddress(HttpSession session, String name, String phone, String detail) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/xiaomi/address/getAddress");
        User user = (User) session.getAttribute("user");
        //判断是否登录
        if (user == null) {
            mv.setViewName("redirect:/login.jsp");
            return mv;
        }
        if (name.isEmpty()) {
            mv.addObject("msg", "收件人不能为空");
            return mv;
        }
        if (phone.isEmpty()) {
            mv.addObject("msg", "电话不能为空");
            return mv;
        }
        if (detail.isEmpty()) {
            mv.addObject("msg", "详细地址不能为空");
            return mv;
        }
        Address address = new Address(null, detail, name, phone, user.getId(), 0);
        int num = addressService.addAddress(address);

        return mv;
    }

    @GetMapping(value = "/defaultAddress", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView defaultAddress(HttpSession session, int id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/xiaomi/address/getAddress");
        User user = (User) session.getAttribute("user");
        //判断是否登录
        if (user == null) {
            mv.setViewName("redirect:/login.jsp");
            return mv;
        }
        int num = addressService.updatedefault(user.getId(), id);
        return mv;
    }

    //删除地址2020 6 5
    @GetMapping(value = "/deleteAddress", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView deleteAddress(HttpSession session, int id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/xiaomi/address/getAddress");
        User user = (User) session.getAttribute("user");
        //判断是否登录
        if (user == null) {
            mv.setViewName("redirect:/login.jsp");
            return mv;
        }
        int num = addressService.deleteAddress(id);
        return mv;
    }


    @PostMapping(value = "/updateAddress", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView updateAddress(HttpSession session,Address address) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/xiaomi/address/getAddress");
        User user = (User) session.getAttribute("user");
        //判断是否登录
        if (user == null) {
            mv.setViewName("redirect:/login.jsp");
            return mv;
        }
        address.setUid(user.getId());
        System.out.println(address);
        int num=addressService.updateAddress(address);
        return mv;
    }

}


